#include<iostream>
using namespace std;
  
class Monster;
class Bug;
 
class Atm
{
  public:
     Atm(int); //初始化奥特曼
    
     void display(); //展示各自属性
    
     int escape();  //逃跑
    
     int attack(Atm&a, Monster&b);  //攻击
    
     int attacked(Monster&b); //被攻击
          
     int restore();  //补血
    
     int win(Atm&a, Monster&b, int, char, Bug&c);  //胜利
    
     int upgrade(); //升级
       
     int getrank();
  
     int gethp();
       
     int getdamage();
  
     int getmoney();
    
  private:
     int rank;
     int exp;
     int hp;
     int damage;
     int money;
};
    
class Monster
{
  public:
     Monster(int, char); // 初始化怪兽
 
     void display();
  
     int attacked(Atm&a, Monster&b); //怪兽被攻击
         
     int fightback(Atm&a, Monster&b); //怪兽反击
  
     int gethp();
       
     int getdamage();
  
     int getmoney();
  
     int getexp();
    
  private:
     int rank;
     int hp;
     int damage;
     int money;
     int exp;
};
 
class Bug
{
    public:
      Bug(int);
 
      int getmoney();
 
      int getexp();
 
    private:
      int money;
      int exp;
};
 
Bug::Bug(int num)
{
     money = num;
     exp = num;
}
 
int Bug::getmoney()
{
   return money;
}
 
int Bug::getexp()
{
   return exp;
}
    
Atm::Atm(int ra=1)
{ 
     rank = ra;
     hp = rank*10;
     damage = rank*3;
     money = rank*10;
     exp = 0;
}
    
void Atm::display()
{ 
    cout << "rank=" << rank << " " << "hp=" << hp << " " << "damage=" << damage << " "
         << "exp=" << exp << " " << "money=" << money << endl;
}
    
int Atm::escape()
{
   money = 0;
   cout << "lose" << endl;
  
   return 0;
}
 
int Atm::attack(Atm&a, Monster&b)
{
   b.attacked(a, b);
   return 0;
}
    
int Atm::attacked(Monster&b)
{
   hp = hp - (b.getdamage()/2);
   return 0;
}
    
int Atm::restore()
{
   while(money > 0 && hp <= rank*10)
   { 
       hp = hp + 1;
       money = money - 10;
   }
  
   return 0;
}
    
int Atm::win(Atm&a, Monster&b, int n, char ty, Bug&c)
{
   if(ty == 'N')
   {  
       money = money + b.getmoney();
       exp = exp + b.getexp();
   }
   else
   {
      money = money + b.getmoney() + n*c.getmoney();
      exp = exp + b.getexp() + n*c.getexp();
   }
   cout << "win" << endl;
   a.upgrade();
   return 0;
}
    
int Atm::upgrade()
{
   while(exp >= rank*10)
   {  
       exp = exp - rank*10;
 
       rank = rank + 1;
       hp = rank * 10;
       damage = rank * 3;
   }
 
   return 0;
}
  
int Atm::gethp()
{
   return hp;
}
  
int Atm::getdamage()
{
   return damage;
}
  
int Atm::getmoney()
{
   return money;
}
  
int Atm::getrank()
{
   return rank;
}
    
Monster::Monster(int lv, char ty)
{ 
    if(ty == 'N')
    { 
        rank = lv;
        hp = rank*10;
        damage = rank*2;
        money = rank*10;
        exp = rank*10;
    }
 
    if(ty == 'A')
    { 
        rank = lv;
        hp = rank*5;
        damage = rank*4 + lv;
        money = rank*10;
        exp = rank*10;
    }
 
    if(ty == 'D')
    { 
        rank = lv;
        hp = rank*20 + (5*lv);
        damage = rank*1;
        money = rank*10;
        exp = rank*10;
    }
     
    if(ty == 'M')
    { 
        rank = lv;
        hp = rank*10 + lv;
        damage = rank*1;
        money = rank*20;
        exp = rank*20;
    }
}
  
void Monster::display()
{ 
    cout << "rank=" << rank << " " << "hp=" << hp << " " << "damage=" << damage << " "
         << "exp=" << exp << " " << "money=" << money << endl;
}
  
int Monster::attacked(Atm&a, Monster&b)
{ 
    hp = hp - a.getdamage();
    b.fightback(a, b);
    return 0;
}
    
int Monster::fightback(Atm&a, Monster&b)
{ 
    a.attacked(b);
    return 0;
}
    
int Monster::gethp()
{
   return hp;
}
  
int Monster::getdamage()
{
   return damage;
}
  
int Monster::getmoney()
{
   return money;
}
  
int Monster::getexp()
{
   return exp;
}
 
    
int main()
{ 
   Bug c(5);
   int lvl[100];
   char ty[100];
 
   int t;
  
   cin>>t;   //表示有t个测试实例
  
   while (t--)  //每个实例就是奥特曼与怪兽的1对1战斗
   {
          int ra, n;
          int j;
          
          cin >> ra;
   
          Atm a(ra);
          a.display();  
 
          cin >> n;
 
          for(j=0; j<n; j++)
          {     
              cin >> lvl[j] >> ty[j];
          }
          
          int i = 0;
 
      while(i<n)   
      { 
          Monster b(lvl[i], ty[i]);
 
          bool flag = true;
 
          while(flag) //当战斗未结束就继续
          {   
              a.attack(a, b);
  
              if (b.gethp() > 0)  //用怪兽生命来判断，用get方法啦
              {     
                  if(a.gethp() >= 10)
                  {
                      if((a.gethp() > 10 )&& (a.gethp() <((a.getrank()*10)/2)) )//奥特曼回血，在方法中判断是否需要加血
                          a.restore();
                  }
                  else
                  {   
                      a.escape();
                      a.display();
                      flag = false;   //设置flag为false，停止战斗
                  }
              }
              else //怪兽死了
              {     
                 a.win(a, b, lvl[i], ty[i], c);
                 a.display();  //输出奥特曼状态，调用display方法
                 flag = false;
              }
          }
          if(a.gethp() < 10)
             break;
          i++;
      }
   }
  
   return 0;
} 
    zhong hao xian test
    bu yao field_application
    ma dan shiyancuowu
    gao ding le ,mei shan chu hao
